<?php
class gyxj_points_recordModel extends spModel
{
    public $pk = "id";
    public $table = "gyxj_points_record";

    /**
     * 记录积分变动
     * @param string $user_id 用户ID
     * @param int $change_points 变动积分(正数增加，负数减少)
     * @param string $reason 变动原因(ai_generate/study/share/system)
     * @param string $related_id 关联ID
     * @return bool
     */
    public function recordPointsChange($user_id, $change_points, $reason, $related_id = null)
    {
        $data = [
            'user_id' => $user_id,
            'change_points' => $change_points,
            'reason' => $reason,
            'related_id' => $related_id,
            'created_at' => date('Y-m-d H:i:s')
        ];
        return $this->create($data);
    }

    /**
     * 获取用户积分记录
     * @param string $user_id 用户ID
     * @param int $page 页码
     * @return array
     */
    public function getUserPointsRecord($user_id, $page = 1)
    {
        return $this->spPager($page, 15)->findAll(['user_id' => $user_id], 'created_at DESC');
    }
}
?>